package com.teamcalendar.client.controller;

import java.io.IOException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.ImageFormat;
import android.os.Bundle;
import android.util.Base64;
import android.view.Display;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import com.teamcalendar.api.CreateJson;
import com.teamcalendar.api.ParseJson;
import com.teamcalendar.client.DBAdapter;


public class TeamSiteActivity extends Activity {

	DBAdapter mDb = new DBAdapter(this);
	Bundle bundle;
	String teamName;
	String path;
	String teamIds;
	String teamId;
	Button membersButton,eventsButton;
	long teamIdRow;
	String memberId;
	String role;
	Cursor mCursor;
	String imageUrl;
	String imageName;
	ImageView imageView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.team_site);
		mDb.open();
		bundle = getIntent().getExtras();
		path=bundle.getString("path");

		membersButton=(Button) findViewById(R.id.teamSiteMemberOverviewButton);
		eventsButton=(Button) findViewById(R.id.teamSiteCalendarButton);
		imageView=(ImageView) findViewById(R.id.team_imageView);
		TextView text = (TextView) findViewById(R.id.teamSiteTopic);
		System.out.println("XXXXXXXXXXXXXXXXXX"+String .valueOf(bundle.getLong("teamIdRow")));
		teamId = bundle.getString("teamId");
		if(path.equals("myTeam") && bundle.getLong("teamIdRow")==0){
			
			mCursor = mDb.getMyTeam(teamId);
			mCursor.moveToFirst();
			teamName = mCursor.getString(mCursor.getColumnIndex(DBAdapter.KEY_NAME));
			imageName = mCursor.getString(mCursor.getColumnIndex(DBAdapter.KEY_IMAGE));
			memberId = bundle.getString("memberId");
			mCursor = mDb.getMyTeamMember(memberId);
			role = mCursor.getString(mCursor.getColumnIndex(DBAdapter.KEY_ROLE));
			

		}else{
			mCursor = mDb.getTeam(bundle.getLong("teamIdRow"));
			teamName = mCursor.getString(mCursor.getColumnIndex(DBAdapter.KEY_NAME));
			teamId = mCursor.getString(mCursor.getColumnIndex(DBAdapter.KEY_TEAMIDS));
			imageName = mCursor.getString(mCursor.getColumnIndex(DBAdapter.KEY_IMAGE));
		

			CreateJson createTeamInfoJson = new CreateJson();
			createTeamInfoJson.setJsonObjectType("getteaminfo");
			createTeamInfoJson.setTeamId(teamId);
			createTeamInfoJson.createJsonObject();
			ParseJson getTeamInfo = new ParseJson(mDb);
			getTeamInfo.fromJSONTeamInfo(createTeamInfoJson.getResponse());
			path = "currentTeam";
		}
		mCursor.close();
		text.setText(teamName);
		byte[] teamImage = null;
		if(!imageName.equals("")){
				mCursor=mDb.getImage(imageName);
				mCursor.moveToFirst();
				String temp=mCursor.getString(mCursor.getColumnIndex(DBAdapter.KEY_IMAGE));
				teamImage=Base64.decode(temp, Base64.DEFAULT);
		}
		if (teamImage!=null){
			Bitmap bitMap=BitmapFactory.decodeByteArray(
					teamImage, 0, teamImage.length);
			Display display = getWindowManager().getDefaultDisplay();
			int maxWidth=display.getWidth();
			int maxHeight=display.getHeight();
			if(maxWidth>maxHeight){
				maxWidth=maxWidth*1/2;
				maxHeight=maxHeight*1/2;
			}else{
				maxWidth=maxWidth*1/2;
				maxHeight=maxHeight*1/4;
			}
			imageView.setImageBitmap(resizeImage(bitMap, maxWidth, maxHeight));
		
//			imageView.setImageBitmap(BitmapFactory.decodeByteArray(teamImage, 0, teamImage.length));
	
		}
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		mDb.close();
	};


	public void memberOverview(View view) {
		Intent memberOverview = new Intent(TeamSiteActivity.this, MemberOverviewActivity.class);
		memberOverview.putExtra("path", path);
		memberOverview.putExtra("memberId", memberId);
		
		startActivity(memberOverview);
	}

	public void calendar(View view) {
		Intent calendar = new Intent(TeamSiteActivity.this, CalendarActivity.class);
		calendar.putExtra("teamId", teamId);
		calendar.putExtra("role", role);
		calendar.putExtra("path", path);
		startActivity(calendar);
	}

	
	
	private Bitmap resizeImage( final Bitmap image, final int maxWidth, final int maxHeight ) {
		final Bitmap resizedImage;
		int imageHeight = image.getHeight();
		if ( imageHeight > maxHeight )
		imageHeight = maxHeight;
		int imageWidth = (imageHeight*image.getWidth()) / image.getHeight();
		if ( imageWidth > maxWidth ) {
		imageWidth = maxWidth;
		imageHeight = (imageWidth*image.getHeight()) / image.getWidth();
		}
		resizedImage = Bitmap.createScaledBitmap( image, imageWidth, imageHeight, true);
		return resizedImage;
		} 

}
